#include"Stack.h"

void StackInit(Stack* ps)
{
	assert(ps);
	ps->a = (STDataType*)malloc(sizeof(STDataType) * 4);
	if (ps == NULL)
	{
		perror("malloc fail!");
		return;
	}
	ps->top = -1;
	ps->capacity = 4;
}

void StackPush(Stack* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * (ps->capacity) * 2);
		if (tmp == NULL)
		{
			perror("malloc fail!");
			return;
		}
		ps->a = tmp;
		ps->capacity *= 2;
	}
	ps->top++;
	ps->a[ps->top] = x;
}

void StackPop(Stack* ps)
{
	assert(ps);
	assert(!StackisEmpty(ps));
	ps->top--;
}

int StackSize(Stack* ps)
{
	assert(ps);
	int Top = ps->top;
	Top++;
	return Top;
}

bool StackisEmpty(Stack* ps)
{
	assert(ps);
	return ps->top == -1;
}

STDataType StackTop(Stack* ps)
{
	assert(ps);
	return ps->a[ps->top];
}

void StackDestroy(Stack* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = -1;
	ps->capacity = 0;
}